package com.example.mybatis.service.impl;

import com.example.mybatis.mapper.UserTMapper;
import com.example.mybatis.pojo.UserT;
import com.example.mybatis.pojo.UserTExample;
import com.example.mybatis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserTMapper userTMapper;

    @Override
    @Transactional
    public void addUser(UserT userT) {
        this.userTMapper.insert(userT);
    }

    @Override
    public List<UserT> getAllUsers() {
        UserTExample userTExample = new UserTExample();

        return this.userTMapper.selectByExample(userTExample);
    }

    @Override
    public UserT getUserByUserName(String userName) {
        UserTExample userTExample = new UserTExample();
        userTExample.createCriteria().andUserNameEqualTo(userName);
        return this.userTMapper.selectByExample(userTExample).get(0);
    }


    @Override
    @Transactional
    public void updateUser(UserT user) {
        /**
         * 不可以修改主键，因为是根据主键来进行更新操作
         * 这个故事告诉我们，主键不可以是userName，否则用户名更新不了
         */
        this.userTMapper.updateByPrimaryKey(user);
    }

    @Override
    @Transactional
    public void deleteUser(String userName) {
        UserTExample userTExample = new UserTExample();
        userTExample.createCriteria().andUserNameEqualTo(userName);
        this.userTMapper.deleteByExample(userTExample);
    }
}
